﻿/*
 * fbasync - An Asynchronous Facebook API for .NET
 * Copyright (C) 2007 ittybittysoft
 * Email: info@ittybittysoft.com
 * Portions of the library are based on the Facebook Developer Toolkit
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Xml;

namespace fbasync
{
    [DataContract, Serializable]
    public class StoryTemplate
    {
        [DataMember(Name = "template_title")]
        public string template_title { get; set; }

        [DataMember(Name = "template_body")]
        public string template_body { get; set; }

        public static StoryTemplate FromElement(XmlElement e)
        {
            if (null == e) return null;

            var title = e["template_title", FacebookConnection.FacebookNamespace];
            if (null == title)
            {
                return new StoryTemplate { template_title = e.InnerText };
            }
            else
            {
                var body = e["template_body", FacebookConnection.FacebookNamespace];
                return new StoryTemplate { template_title = title.InnerText, template_body = body.InnerText };
            }
        }

        public static IEnumerable<StoryTemplate> FromContainerElement(XmlElement e)
        {
            if (null == e) yield break;

            foreach (XmlNode c in e.ChildNodes)
            {
                //handle either full temmplates or those with no title/body
                if (c.NodeType == XmlNodeType.Element)
                {
                    yield return FromElement((XmlElement)c);
                }
            }
        }
    }
}
